package com.hp.controller;

import com.hp.annotation.Power;
import com.hp.entity.User;
import com.hp.result.DataGridResultInfo;
import com.hp.result.SubmitResultInfo;
import com.hp.service.RoleService;
import com.hp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Power(name = "用户管理",type =0)
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /**
     * 查询所有
     * @return
     */
    @Power(name = "查询所有",type =1,url = "/user/queryuser_result")
    @RequestMapping("/queryuser_result")
    @ResponseBody
    public DataGridResultInfo queryuser_result(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows) throws Exception {

        return userService.page(page, rows);

    }

    /**
     *
     * 去查询页面
     * @return
     */
    @Power(name = "去查询页面",type =1,url = "/user/queryuser")
    @GetMapping(value = "/queryuser")
    public String queryuser() {
        return "user/queryuser";
    }

    @Power(name = "去添加页面",type =1,url = "/user/toadduser")
    @GetMapping("/toadduser")
    public String toadduser(){

        return "user/adduser";
    }

    @Power(name = "用户添加",type =1,url = "/user/addusersubmit")
    //用户添加
    @ResponseBody
    @PostMapping("/addusersubmit")
    public SubmitResultInfo addusersubmit(User user) throws Exception{

        return userService.adduser(user);
    }
    @Power(name = "授权页面",type =1,url = "/user/toauthrole")
    @GetMapping("/toauthrole")
    public String toauthrole(int id, Model model){
        model.addAttribute("roles",roleService.findAll());//查询所有角色
        model.addAttribute("user",userService.findById(id));//查询用户
        return "user/authrole";
    }


    @Power(name = "授权",type =1,url = "/user/authrole")
    @ResponseBody
    @PostMapping("/authrole")
    public SubmitResultInfo authrole(int userid,int roleid){

        return userService.authrole(userid,roleid);//授权
    }



    @Power(name = "退出登录",type =1,url = "/user/loginout")
    @RequestMapping("/loginout")
    public void loginout(HttpServletResponse response, HttpSession session) throws Exception{
        session.removeAttribute("user");
        session.removeAttribute("r");
        session.removeAttribute("menusList");
        session.removeAttribute("powerList");
        response.sendRedirect("http://localhost/tk/login.jsp");
    }


    @Power(name = "去修改密码页面",type =1,url = "/user/updatePwd")
    @RequestMapping("/updatePwd")
    public String updatePwd() {
        return "user/PwdUpdate";
    }



    @Power(name = "去修改密码页面",type =1,url = "/user/updateUserPwd")
    @RequestMapping("/updateUserPwd")
    public void updateUserPwd(HttpServletResponse response, HttpSession session,User user) throws Exception {
        int res=userService.updatePwd(user);
        System.out.println("修改状态:"+res);
        session.removeAttribute("user");
        session.removeAttribute("r");
        session.removeAttribute("menusList");
        session.removeAttribute("powerList");
        response.sendRedirect("http://localhost/tk/login.jsp");
    }

}
